﻿// $Id: ActivityControllers.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 NourY Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.UI.Web.ActivityControllers.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

#if (!NET_CF)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Web;

namespace Salamanca.DataActivities.UI.Web
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>A <see cref="System.Web.SessionState.HttpSessionState" /> stored activity controllers stack.</summary>
    /// <remarks>This class is a <a href="http://www.dofactory.com/Patterns/PatternSingleton.aspx">Singleton</a>.</remarks>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public sealed class ActivityControllersSessionStack
    {

        /// <summary>Initializes a new <see cref="ActivityControllersSessionStack" /> instance.</summary>
        private ActivityControllersSessionStack()
        {
            _SessionKey=string.Format(
                "{0}:{1}",
                GetType(),
                Guid.NewGuid()
            );
        }

        /// <summary>Clears the stack.</summary>
        public void Clear()
        {
            Trace.WriteLine("Clearing controller stack");

            Stack.Clear();
        }

        /// <summary>Pushes the specified <paramref name="controller" /> onto the stack.</summary>
        public void Push(IActivityController controller)
        {
            Trace.WriteLine(
                string.Format(
                    CultureInfo.CurrentCulture,
                    "Pushing controller {0}",
                    controller
                )
            );
            //if (!Stack.Contains(controller))
                Stack.Push(controller);
        }

        /// <summary>Pops the last activity controller from the stack.</summary>
        public IActivityController Pop()
        {
            Trace.WriteLine("Popping controller");

            return (IActivityController)Stack.Pop();
        }

        /// <summary>Gets the last activity controller from the stack.</summary>
        public IActivityController Peek()
        {
            return (IActivityController)Stack.Peek();
        }

        /// <summary>Gets the stack.</summary>
        private Stack Stack
        {
            get
            {
                Stack ret=HttpContext.Current.Session[_SessionKey] as Stack;
                if (ret==null)
                {
                    ret=System.Collections.Stack.Synchronized(new Stack());
                    HttpContext.Current.Session[_SessionKey]=ret;
                }
                return ret;
            }
        }

        /// <summary>Gets the unique <see cref="ActivityControllersSessionStack" /> instance.</summary>
        public static ActivityControllersSessionStack Instance
        {
            get
            {
                if (_Instance==null)
                    _Instance=new ActivityControllersSessionStack();
                return _Instance;
            }
        }

        private static ActivityControllersSessionStack _Instance;
        private string _SessionKey;

    }
}
#endif
